﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using ArticleManager.DataClasses;
using ArticleManager.Forms;


namespace ArticleManager.Forms
{
    public partial class frmMain : Form
    {
        Controller _controller;
        IList articles;

        public RichTextBox RichTextBox
        {
            get
            {
                return this.strArticle;
            }
        }

        private void PopulateArtciles()
        {
            foreach (ArticleTitle articleTitle in articles)
            {
                int nInex = ctlTitles.Items.Add(articleTitle);
            }
        }


        public frmMain()
        {
            InitializeComponent();
            _controller = Controller.Instance;
            _controller.ArticleForm = this;
            articles = _controller.GetArticleTitles();

            titAuthors.Text = "";
            titKeywords.Text = "";
            titPublishers.Text = "";
            titReviewers.Text = "";
            titTranslators.Text = "";
            titKeywords.Text = "";
            lblTitle.Text = "Please select an article to view the contents.";


            PopulateArtciles();

        }

        public void AddArticleToList(int id, string title)
        {
            if (id != 0)
            {
                ctlTitles.Items.Add(new ArticleTitle(id, title));
            }
        }

        private void ctlTitles_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            string strKeyWords = "";
            string strAuthors = "";
            ArticleTitle articleTitle = ctlTitles.SelectedItem as ArticleTitle;
            if (articleTitle != null)
            {
                Article article = _controller.GetArticleDetails(articleTitle.ArticleId);

                foreach (ArticleKey key in article.KeywordList)
                {
                    strKeyWords += _controller.GetKeyText(key.KeyId);
                    strKeyWords += ",";
                }

                //Remove the last comma.
                strKeyWords.Trim();
                if (strKeyWords.EndsWith(","))
                {
                    strKeyWords = strKeyWords.TrimEnd(',');
                }

                foreach (ArticleAuthor author in article.AuthorsList)
                {
                    strAuthors += _controller.GetAuthorName(author.AuthorId);
                    strAuthors += ",";
                }

                //Remove the last comma.
                strAuthors.Trim();
                if (strAuthors.EndsWith(","))
                {
                    strAuthors = strAuthors.TrimEnd(',');
                }

                titPublishers.Text = article.Publishers;
                titReviewers.Text = article.Reviewers;
                titTranslators.Text = article.Translators;
                lblTitle.Text = article.Title;
                strArticle.Rtf = article.Text;
                titKeywords.Text = strKeyWords;
                titAuthors.Text = strAuthors;
            } //end of if artice is not null.
        }

        private void strpBtnArtNew_Click(object sender, EventArgs e)
        {
            frmNewArticle editor = new frmNewArticle();
            editor.Show();
        }

        private void mnuArtNew_Click(object sender, EventArgs e)
        {
            frmNewArticle editor = new frmNewArticle();
            editor.Show();
        }

        private void mnuMngAuthors_Click(object sender, EventArgs e)
        {
            frmManageAuthorsForm frm = new frmManageAuthorsForm();
            frm.Show();
        }

        private void mnuManKey_Click(object sender, EventArgs e)
        {
            frmManageKeysForm frm = new frmManageKeysForm();
            frm.Show();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAboutBox frm = new frmAboutBox();
            frm.Show();
        }

        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmFind frm = new frmFind(this);
            frm.Show();

        }

        private void replaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmReplace frm = new frmReplace(this);
            frm.Show();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            frmFind frm = new frmFind(this);
            frm.Show();
        }

        private void mnuArtEdit_Click(object sender, EventArgs e)
        {
            //string strKeyWords = "";
            //string strAuthors = "";
            ArticleTitle articleTitle = ctlTitles.SelectedItem as ArticleTitle;
            if (articleTitle != null)
            {
                Article article = _controller.GetArticleDetails(articleTitle.ArticleId);
                frmNewArticle frm = new frmNewArticle();
                //frm.Article = article;
                frm.EditMode = true;
                frm.DisplayArticle(article);
                frm.Show();
            }
        }
    }
}
